<template>
	<view class="font nextTitle">
		<view v-if="type">
			<view class="min-switch">
			  <text class="text">按选项跳题</text>
			  <switch
				:checked="isDisable"
				@change="optionToJumpTheTopic"
				color="#80b156"
				style="transform: scale(0.7,0.7)"
			  />
			</view>
			<view class="Divider" />
			<view v-show="isDisable" class="content">
				<view class="subject" v-for="(item,index) in optionArray" :key="index">
					<uni-badge class="uni-badge-left-margin" text="0" :is-dot="true" size="small" type="info" />{{item.content}}
					<uni-data-select
					  v-model="item.nextTitleId"
					  :localdata="questionsItems"
					  :identifyTheTopic="index"
					  placeholder="请选择需要跳转的题目"
					></uni-data-select>
				</view>
			</view>
		</view>
		<view class="min-switch juli">
		  <text class="text">无条件跳题</text>
		  <switch
			:checked="isDisable2"
			@change="UnconditionalSkip"
			color="#80b156"
			style="transform: scale(0.7,0.7)"
		  />
		</view>
		<view class="Divider" />
		<view v-show="isDisable2" class="content">
			<text style="font-size: 25rpx;color: #999a9a;margin: 30rpx 30rpx;">做完此题直接跳到:</text>
			<uv-radio-group 
				v-model="radiovalue" 
				placement="column" 
				iconPlacement="right"
				activeColor="#80b156">
				<view v-for="(item,index) in questionsItems" :key="index" class="radio">
					<uv-radio :name='item.value' :label="item.text"></uv-radio>
				</view>
			</uv-radio-group>
		</view>
		<button @click="finish">完成</button>
	</view>
</template>

<script>
import { empty } from '../../uni_modules/uv-ui-tools/libs/function/test';
	export default{
		data() {
			return {
				value: [], //选项选中后存的数据
				optionArray:[], //题目的选项数据
				questionsItems:[], //接口获取题目数据
				isDisable:true, //选项跳题
				isDisable2:false, //题目跳题
				radiovalue:"", //无条件跳题选中的题目id
				subjectId:"",// 这道题的题目id
				type:true
			}
		},
		onLoad: function(option){
			console.log(option);
			// 判断是否是多选和填空
			if(option.type){
				this.type = false
			}
			// 获取该题以下题目信息
			let data = {
				subjectId:option.subjectId
			}
			this.$request('/QuestionTitle/jumpquestiontitle','GET',data)
			.then(res=>{
				for (var i = 0; i < res.data.data.length; i++) {
					res.data.data[i] = {
						value:res.data.data[i].subjectId,
						text:res.data.data[i].subjectNum+'.'+res.data.data[i].title
					}
				}
				this.questionsItems = res.data.data
			})
			/******** 单选的操作 **********/
			// 单选题的选项数据
			if(option.data){
				this.optionArray = JSON.parse(option.data);
			}
			// 判断跳题类型
			if(option.jumpingType == '题目跳题'){
				this.isDisable2 = true
				this.isDisable = false
				this.radiovalue = option.radiovalue
			}
			else if(option.jumpingType == '选项跳题'){
				this.isDisable = true
				this.isDisable2 = false
			}
			else{
				this.isDisable = false
				this.isDisable2 = false
			}
			/******************************/
			if(option.jumpId){
				this.isDisable = false
				this.isDisable2 = true
				this.radiovalue = option.jumpId
			}
		},
		methods:{
			finish(){
				// 填空和多选的返回值
				if(!this.type){
					if(empty(this.radiovalue)){
						uni.showModal({
							content:"请选择题目或者关闭跳题按钮",
							showCancel:false,
							confirmText:"知道了"
						})
						return;
					}
					if(!this.isDisable2){
						this.radiovalue = ''
					}
					var pages = getCurrentPages();
					var prevPage = pages[pages.length - 2]; //上一个页面
					prevPage.setData({
					        radiovalue:{
								"subjectId":this.radiovalue, //所选的题目id
								"name":this.isDisable2?"已设置":"未设置"
							}
					})
					uni.navigateBack()
				}
				// 单选题的返回值
				else{
					var pages = getCurrentPages();
					var prevPage = pages[pages.length - 2]; //上一个页面
					prevPage.setData({
					        radiovalue:
							this.isDisable?
							this.optionArray:
							{
								"subjectId":this.radiovalue, //所选的题目id
								// "name":empty(this.radiovalue)?"未设置":"已设置"
							},
							type:this.isDisable?"选项跳题":this.isDisable2?"题目跳题":"不跳题"
					})
					uni.navigateBack()
				}
			},
			UnconditionalSkip:function(e){
				if(this.isDisable){
					this.isDisable = false
				}
				this.isDisable2 = e.detail.value
			},
			optionToJumpTheTopic:function(e){
				if(this.isDisable2){
					this.isDisable2 = false
				}
				this.isDisable = e.detail.value
			},
		}
	}
</script>

<style lang="less">
	page{
		background-color: #f3f4f5;
	}
	button{
		position: fixed;
		left: 5%;
		width: 90%;
		bottom: 0;
		margin-bottom: 20rpx;
		background-color: #7abb17;
		color: white;
	}
	.min-switch {
	  height: 80rpx;
	  font-size: 32rpx;
	  margin: 0 30rpx;
	  padding: 20rpx 0;
	  display: flex;
	  justify-content: space-between;
	  align-items: center;
	  background: #fff;
	  border-radius: 15rpx;
	  .text{
	    padding-left: 30rpx
	  }
	}
	.content{
		height: auto;
		margin: 0 30rpx;
		padding: 20rpx 0;
		// display: flex;
		// justify-content: space-between;
		// align-items: center;
		background: #fafbfc;
		border-radius: 0 0 15rpx 15rpx;
		// .text{
		//   padding-left: 30rpx
		// }
	}
	.juli{
		margin-top: 20rpx;
	}
	.Divider {
	  border-top: 1px solid #eee;
	  border-bottom: 1px solid #eee;
	}
	.radio{
		margin: 20rpx 30rpx;
	}
</style>